<?php
/**
 * File : TranslationDAO.php
 * Created by Ludovic Reenaers
 * Created on  4 nov. 2010
 * lreenaers@hotmail.com
 */
class TranslationDAO extends DAO{
	public function getById($id){
		try
		{
			$id =Validator::escapeSql($id);
			$query="select t.ID AS ID, t.LABEL AS LABEL, w.NAME AS NAME from TRANSLATION as t,WORDS as w where t.WORD_ID=w.ID and t.ID=$id;";
				
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = new Translation((int)$Bean->ID);
				$thebean->setName($Bean->NAME);
				$thebean->setLabel($Bean->LABEL);
			}
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $thebean;
	}
	function addByLngId($Bean,$lngId){
		try{

			$insertQuery = "insert into WORDS (NAME) values ('".Validator::escapeSql($Bean->getName())."');";
			$this->pdo->exec($insertQuery);
			$nameId= $this->pdo->lastInsertId();
			$nameId = Validator::escapeSql($nameId);
			$lngId = Validator::escapeSql($lngId);
			$insertQuery = "insert into TRANSLATION (WORD_ID,LNG_ID,LABEL) values ($nameId,$lngId,'".Validator::escapeSql($Bean->getLabel())."');";
			$this->pdo->exec($insertQuery);
			$thebean = $this->getById((int)$this->pdo->lastInsertId());

		}catch(Exception $e)    {
			$this->pdo->message;
		}
		return $thebean;
	}
	function getAllByIso($Iso){
		try
		{
			$Iso = Validator::escapeSql($Iso);
			$BeanArray = array();
			$query="SELECT t.ID AS ID ";
			$query.="FROM TRANSLATION AS t, WORDS AS w, LANGUES AS l ";
			$query.="WHERE t.WORD_ID = w.ID AND t.LNG_ID = l.ID AND l.ISO='$Iso';";
				
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = $this->getById((int)$Bean->ID);
				array_push($BeanArray,$thebean);
			}

		}
		catch(Exception $e)     {
			throw $e;
		}

		return $BeanArray;

	}
	function update($Bean){
		try{

			$query = "update TRANSLATION set LABEL='".Validator::escapeSql($Bean->getLabel())."' where ID=".Validator::escapeSql($Bean->getId()).";";
			$this->pdo->exec($query);
			$bool=true;
		}
		catch(Exception $e)     {
			$bool=false;

			echo $e;
                        }
                return $bool;
        }        
}
?>